//
//  RootViewController.m
//  ProjectA_V0.1
//
//  Created by liuchang on 14/12/29.
//  Copyright (c) 2014年 liuchang. All rights reserved.
//

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#import "RootViewController.h"
#import "MainViewController.h"
#import "LeftViewController.h"
#import "RightViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.mainView = [[MainViewController alloc] init];
        self.leftView = [[LeftViewController alloc] init];
        self.rightView = [[RightViewController alloc] init];
        UINavigationController * navCon = [[UINavigationController alloc] initWithRootViewController:_mainView];
        [_mainView release];
        [self addChildViewController:navCon];
        [self addChildViewController:_leftView];
        [_leftView release];
        [self addChildViewController:_rightView];
        [_rightView release];
        [self.view addSubview:navCon.view];
        [self.view addSubview:_leftView.view];
        [self.view addSubview:_rightView.view];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setMainView];
    [self setLeftView];
    [self setRightView];
}

- (void)setMainView
{
    _mainView.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"图片1" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    _mainView.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"图片2" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    [_mainView leftGetBlock:^(UIScreenEdgePanGestureRecognizer *recognizer) {
        if (UIGestureRecognizerStateBegan == recognizer.state ||
             UIGestureRecognizerStateChanged == recognizer.state) {
            CGPoint translation = [recognizer translationInView:_leftView.view];
            if (_leftView.view.frame.origin.x + translation.x < 0) {
                _leftView.view.center = CGPointMake(_leftView.view.center.x + translation.x, _leftView.view.center.y);
                [recognizer setTranslation:CGPointMake(0, 0) inView:_leftView.view];
            }
            else {
                [_leftView.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.382, SCREEN_HEIGHT)];
            }
        }
        else {
#warning 结束滑动
            if (_leftView.view.frame.origin.x > -20) {
                [UIView animateWithDuration:0.3 animations:^{
                    [_leftView.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.382, SCREEN_HEIGHT)];
                }];
                _mainView.sideIsShow = YES;
            }
            else {
                [UIView animateWithDuration:0.3 animations:^{
                    [_leftView.view setFrame:CGRectMake(- SCREEN_WIDTH * 0.382, 0, SCREEN_WIDTH * 0.382, SCREEN_HEIGHT)];
                }];
            }
        }
    }];
    [_mainView rightGetBlock:^(UIScreenEdgePanGestureRecognizer *recognizer) {
        if (UIGestureRecognizerStateBegan == recognizer.state ||
            UIGestureRecognizerStateChanged == recognizer.state) {
            CGPoint translation = [recognizer translationInView:_rightView.view];
            if (_rightView.view.frame.origin.x + 0.618 * SCREEN_WIDTH + translation.x > SCREEN_WIDTH) {
                _rightView.view.center = CGPointMake(_rightView.view.center.x + translation.x, _rightView.view.center.y);
                [recognizer setTranslation:CGPointMake(0, 0) inView:_rightView.view];
            }
            else {
                [_rightView.view setFrame:CGRectMake(SCREEN_WIDTH * 0.382, 0, SCREEN_WIDTH * 0.618, SCREEN_HEIGHT)];
            }
        }
        else {
#warning 结束滑动
            if (_rightView.view.frame.origin.x < 0.5f * SCREEN_WIDTH) {
                [UIView animateWithDuration:0.3 animations:^{
                    [_rightView.view setFrame:CGRectMake(SCREEN_WIDTH * 0.382, 0, SCREEN_WIDTH * 0.618, SCREEN_HEIGHT)];
                }];
                _mainView.sideIsShow = YES;
            }
            else {
                [UIView animateWithDuration:0.3 animations:^{
                    [_rightView.view setFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH * 0.618, SCREEN_HEIGHT)];
                }];
            }
        }

    }];
}

- (void)setLeftView
{
    [_leftView endGetBlock:^{
        _mainView.sideIsShow = NO;
    }];
}

- (void)setRightView
{
    [_rightView.view setFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH * 0.618, SCREEN_HEIGHT)];
    [_rightView endGetBlock:^{
        _mainView.sideIsShow = NO;
    }];
}

- (void)leftBarButtonAction:(id)sender
{
    [UIView animateWithDuration:0.5 animations:^{
        [_leftView.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.382, SCREEN_HEIGHT)];
    }];
    _mainView.sideIsShow = YES;
}

- (void)rightBarButtonAction:(id)sender
{
    [UIView animateWithDuration:0.5 animations:^{
        [_rightView.view setFrame:CGRectMake(SCREEN_WIDTH * 0.382, 0, SCREEN_WIDTH * 0.618, SCREEN_HEIGHT)];
    }];
    _mainView.sideIsShow = YES;
}

@end
